<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactStage\Action\Dashboard;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\User\Logic\Traits\UserTrait;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\ContactStage\Contract\ContactStageContract;
use MoChat\Plugin\ContactStage\Contract\ContactStageEmployeeContract;

/**
 * 查看客户详情基本信息.
 *
 * Class Show
 * @Controller
 */
class Show extends AbstractAction
{
    use ValidateSceneTrait;
    use UserTrait;

    /**
     * @Inject
     * @var ContactStageEmployeeContract
     */
    protected $ContactStageEmployeeservice;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeservice;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactService;

    /**
     * @Inject
     * @var ContactStageContract
     */
    protected $contactStageService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/contactStage/show", methods="GET")
     */
    public function handle()
    {
        $corpId = $this->corpId();
        // 接收参数
        $params['contactId'] = $this->request->input('contactId');
        $params['employeeIds'] = $this->request->input('employeeIds');
        // 校验参数
        $this->validated($params);

        $stage = $this->contactStageService->getContactStagesByCorpId($corpId, ['id', 'name']);
        $contactStage = $this->ContactStageEmployeeservice->getContactStageEmployeesByOtherId((int) $params['contactId'], $params['employeeIds'], $corpId);

        $data = [];
        foreach ($contactStage as $k => $v) {
            $employee = $this->workEmployeeservice->getWorkEmployeeById($v['employeeId'], ['id', 'name']);
            $contact = $this->workContactService->getWorkContactById($v['contactId'], ['id', 'name']);
            $data[$k]['employeeId'] = $employee['id'];
            $data[$k]['employeeName'] = $employee['name'];
            $data[$k]['contactId'] = $contact['id'];
            $data[$k]['contactName'] = $contact['name'];
            foreach ($stage as $k1 => $v1) {
                $stage[$k1]['flag'] = false;
                if ($v['stageId'] == $v1['id']) {
                    $data[$k]['current']['id'] = $v1['id'];
                    $data[$k]['current']['name'] = $v1['name'];
                    $stage[$k1]['flag'] = true;
                }
            }
            $data[$k]['list'] = $stage;
        }

        return $data;
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'contactId' => 'required|integer',
            'employeeIds' => 'required',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'contactId.required' => '客户id必传',
            'contactId.integer' => '客户id必须为数字',
            'employeeIds.required' => '员工id必传',
        ];
    }
}
